package com.hdkj.eduservice.controller;

import com.hdkj.commonutils.JwtUtils;
import com.hdkj.commonutils.R;
import com.hdkj.eduservice.entity.EduArtcommont;
import com.hdkj.eduservice.entity.Uartcomm;
import com.hdkj.eduservice.service.ArtCommentService;
import com.hdkj.eduservice.service.ArticleService;
import com.hdkj.eduservice.service.UartcommService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/eduservice/artcommont")
public class ArtCommontController {

    @Autowired
    private ArtCommentService artCommentService;

    @Autowired
    private ArticleService articleService;

//    @Autowired
//    private MesClient mesClient;

    @Autowired
    private UartcommService uartcommService;

    //根据文章id，查看对应的评论
    @RequestMapping("getcommontByaid/{aid}")
    public R getcommontByaid(@PathVariable String aid) {
        List<EduArtcommont> list = artCommentService.list(new QueryWrapper<EduArtcommont>().eq("article_id", aid));
        return R.ok().data("items", list);
    }

    //添加用户对某一文章的评论
    @PostMapping("saveartcommont")
    public R saveartcommont(@RequestBody EduArtcommont eduArtcommont, HttpServletRequest request) {
        Uartcomm uartcomm = new Uartcomm();
        String userid = JwtUtils.getMemberIdByJwtToken(request);
        eduArtcommont.setMemberId(userid);
        eduArtcommont.setCreatTime(new Date());
        eduArtcommont.setMotifTime(new Date());
        boolean save = artCommentService.save(eduArtcommont);

        if (save){
//          mesClient.sendmes(eduArtcommont.getContent(),"artcomm");
//            EduArticle article = articleService.getOne(new QueryWrapper<EduArticle>().eq("id", eduArtcommont.getArticleId()));
            uartcomm.setUserid(userid);
            uartcomm.setUserArtcommid(eduArtcommont.getId());
            uartcommService.save(uartcomm);
            return R.ok();
        }
       return R.error();
    }


}
